from typing import Optional, List
from datetime import datetime
from pydantic import BaseModel, EmailStr, Field


class UserBase(BaseModel):
    """用户基础Schema"""
    email: EmailStr
    username: str = Field(..., min_length=3, max_length=50)
    full_name: Optional[str] = Field(None, max_length=100)
    phone: Optional[str] = Field(None, max_length=20)
    avatar: Optional[str] = None
    is_active: bool = True
    is_verified: bool = False


class UserCreate(UserBase):
    """用户创建Schema"""
    password: str = Field(..., min_length=6, max_length=128)
    role_ids: Optional[List[int]] = []


class UserUpdate(BaseModel):
    """用户更新Schema"""
    email: Optional[EmailStr] = None
    username: Optional[str] = Field(None, min_length=3, max_length=50)
    full_name: Optional[str] = Field(None, max_length=100)
    phone: Optional[str] = Field(None, max_length=20)
    avatar: Optional[str] = None
    is_active: Optional[bool] = None
    is_verified: Optional[bool] = None
    role_ids: Optional[List[int]] = None


class UserInDBBase(UserBase):
    """数据库中的用户基础信息"""
    id: int
    is_superuser: bool = False
    last_login_at: Optional[datetime] = None
    created_at: datetime
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True


class User(UserInDBBase):
    """用户返回Schema"""
    roles: List = []  # 避免前向引用
    permissions: List[str] = []
    profile_completion: Optional[int] = None


class UserInDB(UserInDBBase):
    """数据库中的完整用户信息"""
    hashed_password: str


class UserWithRoles(User):
    """带角色的用户信息"""
    roles: List = []  # 避免前向引用
    permissions: List[str] = []


class UserList(BaseModel):
    """用户列表Schema"""
    items: List[User]
    total: int
    page: int
    size: int